<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="mysql,InnoDB,binlog" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="MySql 文件-binlog二进制日志（binary log）记录了对MySQL数据库执行的更改的所有操作，但是不包括SELECT和SHOW这类操作. 指定要记录binlog的数据库和binlog_ignore_db指定不记录binlog的数据库。对运行中的mysql要启用binlog可以通过命令SET SQL_LOG_BIN=1来设置  物理日志（physical loggin）：物理日志记录">
<meta name="keywords" content="mysql,InnoDB,binlog">
<meta property="og:type" content="article">
<meta property="og:title" content="MySql 文件-binlog">
<meta property="og:url" content="http://www.zhizus.com/2018-07-31-Mysql文件-binlog日志.html">
<meta property="og:site_name" content="郑大侠的博客">
<meta property="og:description" content="MySql 文件-binlog二进制日志（binary log）记录了对MySQL数据库执行的更改的所有操作，但是不包括SELECT和SHOW这类操作. 指定要记录binlog的数据库和binlog_ignore_db指定不记录binlog的数据库。对运行中的mysql要启用binlog可以通过命令SET SQL_LOG_BIN=1来设置  物理日志（physical loggin）：物理日志记录">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-07-31T01:39:54.073Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MySql 文件-binlog">
<meta name="twitter:description" content="MySql 文件-binlog二进制日志（binary log）记录了对MySQL数据库执行的更改的所有操作，但是不包括SELECT和SHOW这类操作. 指定要记录binlog的数据库和binlog_ignore_db指定不记录binlog的数据库。对运行中的mysql要启用binlog可以通过命令SET SQL_LOG_BIN=1来设置  物理日志（physical loggin）：物理日志记录">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://www.zhizus.com/2018-07-31-Mysql文件-binlog日志.html"/>





  <title> MySql 文件-binlog | 郑大侠的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f75c9cec9ecead0dfce9a108b9688072";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">郑大侠的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-crossed">
          <a href="http://zhizus.com" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-camera-retro"></i> <br />
            
            穿越
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-mysql " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.zhizus.com/2018-07-31-Mysql文件-binlog日志.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="DempeZheng">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑大侠的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                MySql 文件-binlog
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            

            

            
          </span>

          

          
            
          

          
          
             <span id="/2018-07-31-Mysql文件-binlog日志.html" class="leancloud_visitors" data-flag-title="MySql 文件-binlog">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="MySql-文件-binlog"><a href="#MySql-文件-binlog" class="headerlink" title="MySql 文件-binlog"></a>MySql 文件-binlog</h2><p>二进制日志（binary log）记录了对MySQL数据库执行的更改的所有操作，但是不包括SELECT和SHOW这类操作.
指定要记录binlog的数据库和binlog_ignore_db指定不记录binlog的数据库。对运行中的mysql要启用binlog可以通过命令SET <code>SQL_LOG_BIN</code>=1来设置</p>
<ul>
<li>物理日志（physical loggin）：物理日志记录每一行改变的内容。</li>
<li>逻辑日志（logical loggin）：逻辑日志记录不是改变的行，而是引起行内容改变的SQL语句（insert ，update ，delete）</li>
</ul>
<p>按照上面的定义binlog文件就应该属于逻辑日志，redo log属于物理日志。</p>
<h2 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h2><ul>
<li>恢复（recovery）：某些数据的恢复需要二进制日志</li>
<li>复制（replication）：通过复制和执行二进制日志使一台远程的MySQL数据库（slave或standby）与一台MysQL数据库（Master）实时同步</li>
<li>审计（audit）：用户可以通过二进制日志中的信息来进行审计，判断是否有对数据库进行注入攻击</li>
</ul>
<h2 id="Binlog的日志格式"><a href="#Binlog的日志格式" class="headerlink" title="Binlog的日志格式"></a>Binlog的日志格式</h2><p>binlog有三种格式：Statement, Row和Mixed.</p>
<ul>
<li>基于SQL语句的复制（statement-based replication, SBR）</li>
<li>基于行的复制（row-based replication, RBR）</li>
<li>混合模式复制（mixed-based replication, MBR）<h3 id="Statement"><a href="#Statement" class="headerlink" title="Statement"></a>Statement</h3></li>
</ul>
<p>每一条会修改数据的sql都会记录在binlog中。</p>
<p>优点：不需要记录每一行的变化，减少了binlog日志量，节约了IO, 提高了性能。</p>
<p>缺点：由于记录的只是执行语句，为了这些语句能在slave上正确运行，因此还必须记录每条语句在执行的时候的一些相关信息，以保证所有语句能在slave得到和在master端执行的时候相同的结果。另外mysql的复制，像一些特定函数的功能，slave可与master上要保持一致会有很多相关问题。</p>
<p>相比row能节约多少性能与日志量，这个取决于应用的SQL情况，正常同一条记录修改或者插入row格式所产生的日志量还小鱼statement产生的日志量，但是考虑到如果带条件的update操作，以及整表删除，alter表等操作，row格式会产生大量日志，因此在考虑是否使用row格式日志时应该根据应用的实际情况，其所产生的日志量会增加多少，以及带来的IO性能问题。</p>
<h3 id="Row"><a href="#Row" class="headerlink" title="Row"></a>Row</h3><p>5.1.5版本的MySQL才开始支持row level的复制,它不记录sql语句上下文相关信息，仅保存哪条记录被修改。</p>
<p>优点： binlog中可以不记录执行的sql语句的上下文相关的信息，仅需要记录那一条记录被修改成什么了。所以row的日志内容会非常清楚的记录下每一行数据修改的细节。而且不会出现某些特定情况下的存储过程，或function，以及trigger的调用和触发无法被正确复制的问题.</p>
<p>缺点:所有的执行的语句当记录到日志中的时候，都将以每行记录的修改来记录，这样可能会产生大量的日志内容。</p>
<blockquote>
<p>新版本的MySQL中对row level模式也被做了优化，并不是所有的修改都会以row level来记录，像遇到表结构变更的时候就会以statement模式来记录，如果sql语句确实就是update或者delete等修改数据的语句，那么还是会记录所有行的变更。</p>
</blockquote>
<h3 id="Mixed"><a href="#Mixed" class="headerlink" title="Mixed"></a>Mixed</h3><p>从5.1.8版本开始，MySQL提供了Mixed格式，实际上就是Statement与Row的结合。 
在Mixed模式下，一般的语句修改使用statment格式保存binlog，如一些函数，statement无法完成主从复制的操作，则采用row格式保存binlog，MySQL会根据执行的每一条具体的sql语句来区分对待记录的日志形式，也就是在Statement和Row之间选择一种。</p>
<h2 id="相关参数"><a href="#相关参数" class="headerlink" title="相关参数"></a>相关参数</h2><h2 id="sync-binlog"><a href="#sync-binlog" class="headerlink" title="sync_binlog"></a>sync_binlog</h2><p><code>sync_binlog=1 or N</code></p>
<blockquote>
<p>This makes MySQL synchronize the binary log’s contents to disk each time it commits a transaction </p>
</blockquote>
<ul>
<li><code>sync_binlog=0</code>:基于操作系统的fysnc()方法同步，性能好，但是容易丢数据；</li>
<li><code>sync_binlog=1</code>:每一个transcation commit都会调用一次fysnc()，能保证数据最安全，但是不能保证完全不丢数据。另外对性能影响较大。</li>
<li><p><code>sync_binlog=N</code>:保证最多丢N条数据     </p>
<p>默认情况下，并不是每次写入时都将binlog与硬盘同步。因此如果操作系统或机器(不仅仅是MySQL服务器)崩溃，有可能binlog中最后的语句丢 失了。要想防止这种情况，你可以使用sync_binlog全局变量(1是最安全的值，但也是最慢的)，使binlog在每N次binlog写入后与硬盘 同步。<strong>即使sync_binlog设置为1,出现崩溃时，也有可能表内容和binlog内容之间存在不一致性。如果使用InnoDB表，MySQL服务器 处理COMMIT语句，它将整个事务写入binlog并将事务提交到InnoDB中。如果在两次操作之间出现崩溃，重启时，事务被InnoDB回滚，但仍 然存在binlog中</strong>。可以用<code>--innodb-safe-binlog</code>选项来增加InnoDB表内容和binlog之间的一致性。(注释：在MySQL 5.1中不需要–innodb-safe-binlog；由于引入了XA事务支持，该选项作废了），该选项可以提供更大程度的安全，使每个事务的 binlog(sync_binlog =1)和(默认情况为真)InnoDB日志与硬盘同步，该选项的效果是崩溃后重启时，在滚回事务后，MySQL服务器从binlog剪切回滚的 InnoDB事务。这样可以确保binlog反馈InnoDB表的确切数据等，并使从服务器保持与主服务器保持同步(不接收 回滚的语句)。</p>
<h2 id="binlog的解析"><a href="#binlog的解析" class="headerlink" title="binlog的解析"></a>binlog的解析</h2><p>binlog事件类型一共有三个版本：</p>
</li>
<li>v1: Used in MySQL 3.23</li>
<li>v3: Used in MySQL 4.0.2 though 4.1</li>
<li>v4: Used in MySQL 5.0 and up<blockquote>
<p>v2出现了很短的时间，并且已经不被支持</p>
</blockquote>
</li>
</ul>
<p>现在所使用的MySQL一般都是5.5起了，所以下面陈述的都是v4版的binlog事件类型:</p>
<h3 id="event的类型"><a href="#event的类型" class="headerlink" title="event的类型"></a>event的类型</h3><p>binlog的事件类型一共有以下几种：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> Log_event_type &#123; </span><br><span class="line">  UNKNOWN_EVENT= <span class="number">0</span>, </span><br><span class="line">  START_EVENT_V3= <span class="number">1</span>, </span><br><span class="line">  QUERY_EVENT= <span class="number">2</span>, </span><br><span class="line">  STOP_EVENT= <span class="number">3</span>, </span><br><span class="line">  ROTATE_EVENT= <span class="number">4</span>, </span><br><span class="line">  INTVAR_EVENT= <span class="number">5</span>, </span><br><span class="line">  LOAD_EVENT= <span class="number">6</span>, </span><br><span class="line">  SLAVE_EVENT= <span class="number">7</span>, </span><br><span class="line">  CREATE_FILE_EVENT= <span class="number">8</span>, </span><br><span class="line">  APPEND_BLOCK_EVENT= <span class="number">9</span>, </span><br><span class="line">  EXEC_LOAD_EVENT= <span class="number">10</span>, </span><br><span class="line">  DELETE_FILE_EVENT= <span class="number">11</span>, </span><br><span class="line">  NEW_LOAD_EVENT= <span class="number">12</span>, </span><br><span class="line">  RAND_EVENT= <span class="number">13</span>, </span><br><span class="line">  USER_VAR_EVENT= <span class="number">14</span>, </span><br><span class="line">  FORMAT_DESCRIPTION_EVENT= <span class="number">15</span>, </span><br><span class="line">  XID_EVENT= <span class="number">16</span>, </span><br><span class="line">  BEGIN_LOAD_QUERY_EVENT= <span class="number">17</span>, </span><br><span class="line">  EXECUTE_LOAD_QUERY_EVENT= <span class="number">18</span>, </span><br><span class="line">  TABLE_MAP_EVENT = <span class="number">19</span>, </span><br><span class="line">  PRE_GA_WRITE_ROWS_EVENT = <span class="number">20</span>, </span><br><span class="line">  PRE_GA_UPDATE_ROWS_EVENT = <span class="number">21</span>, </span><br><span class="line">  PRE_GA_DELETE_ROWS_EVENT = <span class="number">22</span>, </span><br><span class="line">  WRITE_ROWS_EVENT = <span class="number">23</span>, </span><br><span class="line">  UPDATE_ROWS_EVENT = <span class="number">24</span>, </span><br><span class="line">  DELETE_ROWS_EVENT = <span class="number">25</span>, </span><br><span class="line">  INCIDENT_EVENT= <span class="number">26</span>, </span><br><span class="line">  HEARTBEAT_LOG_EVENT= <span class="number">27</span>, </span><br><span class="line">  IGNORABLE_LOG_EVENT= <span class="number">28</span>,</span><br><span class="line">  ROWS_QUERY_LOG_EVENT= <span class="number">29</span>,</span><br><span class="line">  WRITE_ROWS_EVENT_V2 = <span class="number">30</span>,</span><br><span class="line">  UPDATE_ROWS_EVENT_V2 = <span class="number">31</span>,</span><br><span class="line">  DELETE_ROWS_EVENT_V2 = <span class="number">32</span>,</span><br><span class="line">  GTID_LOG_EVENT= <span class="number">33</span>,</span><br><span class="line">  ANONYMOUS_GTID_LOG_EVENT= <span class="number">34</span>,</span><br><span class="line">  PREVIOUS_GTIDS_LOG_EVENT= <span class="number">35</span>, </span><br><span class="line">  ENUM_END_EVENT </span><br><span class="line">  <span class="comment">/* end marker */</span> </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="事件类型分析"><a href="#事件类型分析" class="headerlink" title="事件类型分析"></a>事件类型分析</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">+=====================================+</span><br><span class="line">| event     | timestamp         0 : 4    |</span><br><span class="line">| header  +----------------------------+</span><br><span class="line">|              | type_code         4 : 1    |</span><br><span class="line">|             +----------------------------+</span><br><span class="line">|              | server_id         5 : 4    |</span><br><span class="line">|             +----------------------------+</span><br><span class="line">|              | event_length      9 : 4    |</span><br><span class="line">|             +----------------------------+</span><br><span class="line">|              | next_position    13 : 4    |</span><br><span class="line">|             +----------------------------+</span><br><span class="line">|              | flags            17 : 2    |</span><br><span class="line">|             +----------------------------+</span><br><span class="line">|              | extra_headers    19 : x-19 |</span><br><span class="line">+=====================================+</span><br><span class="line">| event     | fixed part        x : y    |</span><br><span class="line">| data      +----------------------------+</span><br><span class="line">|              | variable part              |</span><br><span class="line">+=====================================+</span><br></pre></td></tr></table></figure>
<h2 id="binlog的应用"><a href="#binlog的应用" class="headerlink" title="binlog的应用"></a>binlog的应用</h2><h3 id="canal"><a href="#canal" class="headerlink" title="canal"></a>canal</h3><p>cannl的本质也是通过协议伪装成slave，然后解析binlog来工作的。</p>
<h3 id="数据恢复脚本"><a href="#数据恢复脚本" class="headerlink" title="数据恢复脚本"></a>数据恢复脚本</h3>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>大侠劫富济贫啦！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/dempe/20180527112524.jpg" alt="DempeZheng WeChat Pay"/>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018-07-29-MySQL的体系结构及存储引擎.html" rel="next" title="MySQL的体系结构及存储引擎">
                <i class="fa fa-chevron-left"></i> MySQL的体系结构及存储引擎
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018-08-1-Mysql的复制-半同步复制.html" rel="prev" title="Mysql的半同步复制（转）">
                Mysql的半同步复制（转） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466"
               alt="DempeZheng" />
          <p class="site-author-name" itemprop="name">DempeZheng</p>
           
              <p class="site-description motion-element" itemprop="description">請更固執，將自己謀生與擅長的事情做到最好</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">68</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">23</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/dempeZheng" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://music.163.com/#/user/home?id=6758793" target="_blank" title="网易云音乐">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  网易云音乐
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/dempe" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.wolfbe.com/" title="朗度云" target="_blank">朗度云</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#MySql-文件-binlog"><span class="nav-number">1.</span> <span class="nav-text">MySql 文件-binlog</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#作用"><span class="nav-number">2.</span> <span class="nav-text">作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Binlog的日志格式"><span class="nav-number">3.</span> <span class="nav-text">Binlog的日志格式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Statement"><span class="nav-number">3.1.</span> <span class="nav-text">Statement</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Row"><span class="nav-number">3.2.</span> <span class="nav-text">Row</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mixed"><span class="nav-number">3.3.</span> <span class="nav-text">Mixed</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#相关参数"><span class="nav-number">4.</span> <span class="nav-text">相关参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#sync-binlog"><span class="nav-number">5.</span> <span class="nav-text">sync_binlog</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#binlog的解析"><span class="nav-number">6.</span> <span class="nav-text">binlog的解析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#event的类型"><span class="nav-number">6.1.</span> <span class="nav-text">event的类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#事件类型分析"><span class="nav-number">6.2.</span> <span class="nav-text">事件类型分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#binlog的应用"><span class="nav-number">7.</span> <span class="nav-text">binlog的应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#canal"><span class="nav-number">7.1.</span> <span class="nav-text">canal</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据恢复脚本"><span class="nav-number">7.2.</span> <span class="nav-text">数据恢复脚本</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">DempeZheng</span>
</div>



        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  






  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("dhzMp0HdJLWMCrt04bwybWzH-gzGzoHsz", "gH6FmxJiUaVLLRXQDWpkkIVv");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

</body>
</html>
